package com.gmscosmo.janus.handler;

import com.alibaba.fastjson.JSONObject;
import com.gmscosmo.janus.util.JanusLogUtil;
import com.gmscosmo.request.JanusRequestBody;
import com.gmscosmo.request.Jsep;
import com.gmscosmo.janus.sendMessage.JanusAsyncSender;
import com.gmscosmo.janus.util.CommonUtils;
import com.gmscosmo.common.JanusNativeSignaling;
import com.gmscosmo.janus.util.JanusUtil;
import com.gmscosmo.request.JanusRequest;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * ClassName: JanusHandler
 * Description: 
 * Date: 2021/10/12 10:54
 * @author stephen.qiu
 * @version 1.0
 *
 */
public abstract class JanusHandler {
    @Autowired
    JanusUtil janusUtil;
    @Autowired
    JanusLogUtil janusLogUtil;
    @Autowired
    JanusAsyncSender janusAsyncSender;
    /**
     * 创建janus session
     * @param userId
     * @param nodeId
     * @return
     * @throws Exception
     */
    public long createSession(String userId, int nodeId) throws Exception {
        JanusRequest createRequest = new JanusRequest();
        createRequest.setJanus(JanusNativeSignaling.CREATE);
        String transaction = CommonUtils.randomString(12);
        createRequest.setTransaction(transaction);
        JSONObject response = janusAsyncSender.sendWithFuture(transaction, false, JSONObject.toJSON(createRequest).toString(), nodeId).get();
        JSONObject data = response.getJSONObject("data");
        long session_id = data.getLong("id");
        janusUtil.addEventListener(userId, session_id);
        return session_id;
    }

    /**
     * 添加janus 插件
     * @param session_id
     * @param nodeId
     * @param plugin
     * @return
     * @throws Exception
     */
    public long createHandle(long session_id, int nodeId,String plugin) throws Exception {
        JanusRequest attachRequest = new JanusRequest();
        attachRequest.setJanus(JanusNativeSignaling.ATTACH);
        attachRequest.setPlugin(plugin);
        String transaction = CommonUtils.randomString(12);
        attachRequest.setSession_id(session_id);
        attachRequest.setTransaction(transaction);
        JSONObject response = janusAsyncSender.sendWithFuture(transaction, false, JSONObject.toJSON(attachRequest).toString(), nodeId).get();
        JSONObject data = response.getJSONObject("data");
        long handle_id = data.getLong("id");
        return handle_id;
    }

    /**
     * 开始拉流
     * @param startData
     * @param nodeId
     * @return
     * @throws Exception
     */
    public String start(JSONObject startData, int nodeId) throws Exception{
        startData = startData.getJSONObject("body");
        long session_id = startData.getLong("session_id");
        long handle_id = startData.getLong("handle_id");

        JSONObject start_body = startData.getJSONObject("body");
        JSONObject jsepData = startData.getJSONObject("jsep");
        Jsep jsep = new Jsep();
        jsep.setSdp(jsepData.getString("sdp"));
        jsep.setType(jsepData.getString("type"));

        JanusRequest offerRequest = new JanusRequest();
        offerRequest.setJanus(JanusNativeSignaling.MESSAGE);
        String transaction = CommonUtils.randomString(12);
        offerRequest.setTransaction(transaction);
        offerRequest.setJsep(jsep);

        JanusRequestBody startBody = new JanusRequestBody();
        startBody.setRequest(start_body.getString("request"));
        startBody.setRoom(Integer.valueOf(start_body.getString("room").split("\\|")[1]));

        offerRequest.setBody(startBody);
        offerRequest.setSession_id(session_id);
        offerRequest.setHandle_id(handle_id);
        JSONObject response = janusAsyncSender.sendWithFuture(transaction, false, JSONObject.toJSON(offerRequest).toString(), nodeId).get();
        return response.toString();
    }
}
